import 'dart:ui' as ui;

import 'package:elmer_tools/common/Utils.dart';
import 'package:flutter/material.dart';

class StcWallperItem {
    final ui.Image image;
    final int index;
    final Color color;
    const StcWallperItem(
        this.image,
        this.index,
        this.color
    );
}

class WCBackground extends CustomPainter {
    final ui.Image image1;
    final ui.Image image2;
    final ui.Image image3;
    final ui.Image image4;
    final double backgroundWidth = 720;
    final double backgroundHeight = 1280;

    final int activeIndex;
    final int time;

    final Paint lightPaint = Paint()
        ..isAntiAlias = true
        ..imageFilter = ui.ImageFilter.blur(sigmaX: 2, sigmaY: 2);
    final bool isTick;

    final Paint btnPaint = Paint()..color=const Color.fromARGB(124, 200, 200, 200);

    List<StcWallperItem> data = [];
 
    WCBackground({
        required this.image1,
        required this.image2,
        required this.image3,
        required this.image4,
        required this.time,
        required this.isTick,
        this.activeIndex = 2
    }):
        data = [
            StcWallperItem(image1, 0, const Color.fromARGB(255, 118,171,193)),
            StcWallperItem(image2, 0, const Color.fromARGB(255, 255,114,114)), 
            StcWallperItem(image3, 0, const Color.fromARGB(255, 21,24,57)),
            StcWallperItem(image4, 0, const Color.fromARGB(255, 130,217,212))
        ],
        super()
    ;
    @override
    void paint(Canvas canvas, Size size) {
        double desHeight = Utils.calcHeight(size.width, backgroundWidth, backgroundHeight);
        Rect srcRect = const Offset(0,0) & Size(backgroundWidth, backgroundHeight);
        Rect desRect = Offset(0, size.height - desHeight) & Size(size.width, desHeight);
        Rect fillRect = const Offset(0,0) & Size(size.width, size.height);
        StcWallperItem item = data[activeIndex];
        Paint fillPaint = Paint()..color=item.color;
    
        canvas.drawRect(fillRect,fillPaint);
        canvas.drawImageRect(item.image, srcRect, desRect, lightPaint);
        // // 绘制倒计时
        // String? fontName = isTick ? "宋体" : "Liehuo";
        // TextStyle txtStyle = TextStyle(
        //             fontSize: 18,
        //             decoration: TextDecoration.none,
        //             fontFamily: fontName,
        //             color: Colors.white
        //         );
        // TextPainter txtPainter = TextPainter()
        //     ..text = TextSpan(text: isTick ? time.toString() : "跳过", style: txtStyle)
        //     ..textDirection = TextDirection.ltr
        //     ..layout(minWidth: size.width);
        // double topLeftX = size.width - 60, topLeftY = 45;
        // double rightBottomX = topLeftX + 50, rightBottomY = topLeftY + 30;
        // double txtLeft = isTick ? topLeftX + 18 : topLeftX + 8;
        // canvas.drawRRect(RRect.fromLTRBR(topLeftX, topLeftY, rightBottomX, rightBottomY, const Radius.circular(30)), btnPaint);
        // txtPainter.paint(canvas, Offset(txtLeft, topLeftY + 5));
        // 绘制族谱
        TextStyle zupuStyle = const TextStyle(
            fontSize: 28,
            fontFamily: "Liehuo",
            decoration: TextDecoration.none,
            color: Colors.white
        );
        TextStyle zupuNStyle = const TextStyle(
            fontSize: 42,
            fontFamily: "Liehuo",
            decoration: TextDecoration.none,
            color: Colors.white
        );
        TextPainter zupuPaint1 = TextPainter()
            ..text = TextSpan(text: "文炳禹庭儒，应邦国有书", style: zupuStyle)
            ..textDirection = TextDirection.ltr
            ..layout();
        TextPainter zupuPaint2 = TextPainter()
            ..text = TextSpan(text: "光明清远大，福显润洪都", style: zupuStyle)
            ..textDirection = TextDirection.ltr
            ..layout();
        TextPainter zupuPaint = TextPainter()
            ..text = TextSpan(text: "莫氏族谱", style: zupuNStyle)
            ..textDirection = TextDirection.ltr
            ..layout();
        zupuPaint1.paint(canvas, Offset((size.width - 300) / 2, 200));
        zupuPaint2.paint(canvas, Offset((size.width - 300) / 2, 240));
        zupuPaint.paint(canvas, Offset((size.width - 160) / 2, 110));
    }
    @override
    bool shouldRepaint(covariant CustomPainter oldDelegate) => true;
}
